package com.hxf.web.action.user;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.jca.context.ResourceAdapterApplicationContext;

import com.hxf.common.DateUtil;
import com.hxf.dao.meal.RestaurantDao;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.RestaurentUserDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

@Results({@Result(name = "success", location = "../common/requestSend.jsp"), @Result(name = "error", location = "../common/error.jsp")})
public class RegisterAction extends WebActionBase{
	private Integer userId; 				// 用户id
	private String userName; 				// 用户名
	private String realName; 				// 真实姓名
	private int userSex; 					// 性别
	private String userIdNo; 				// 身份证号
	private String userMobile;				// 固定电话
	private String userPhone; 				// 手机
	private String userAddress; 			// 地址
	private String userZipCode; 			// 邮编
	private String userEmail; 				// 邮箱
	private String userPwd; 				// 密码
	private Integer userType; 				// 用户类型
	private Integer status; 				// 状态 1.有效 2.审核中 3.审核不通过 4.锁定 5.无效 
	private String createTime; 				// 创建时间 
	private String userRelation;			// 联系人
	private String validateCode;			//验证码
	
	/**
	 * 商家注册
	 * @return
	 */
	public String registerSave() {
		try { 
			Object vcode = getSession().getAttribute("validateCode");
			/**
			 * 验证码
			 */
			if (vcode != null) {
				//判断输入验证码是否正确
				if (!vcode.equals(validateCode)) {
					println("-1");
					return null;
				}
			}
			
			/*UserDto user = new UserDto(userId, userName, realName, null, userIdNo, userMobile, userPhone, userAddress, userZipCode, userEmail, userPwd, userType, status, DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M), userRelation);
			//商家注册
			user.setUserType(UserDto.USER_TYPE_1);
			//审核中
			user.setStatus(UserDto.USER_STATUS_1); 
			user.setRestaurantUserId(UserDto.REGISTER_ADMIN_1);//默认为1
			user.setParentId(UserDto.REGISTER_ADMIN_1);//后台注册的会员，默认在 admin 下
			user.setParentUserId(UserDto.REGISTER_ADMIN_1+"#");//上级 id 编号存储方式 以 # 分开
			super.serviceFacade.getUserService().saveUser(user);//注册添加用户*/
		
			
			
			//添加商家和餐厅管理员用户
			RestaurentUserDto restUserDto = new RestaurentUserDto();
			restUserDto.setName(realName);//餐厅名称
			restUserDto.setAccount(userName);//账户
			restUserDto.setPwd(userPwd);//密码
			restUserDto.setLinkMan(userRelation);//联系人
			restUserDto.setMobile(userPhone);//手机号码
			restUserDto.setPhone(userMobile);// 送餐电话
			restUserDto.setStatus(RestaurentUserDto.STATUS); //审核中
			super.serviceFacade.getRestaurantUserService().save(restUserDto);//餐厅管理员注册
			
			RestaurentUserDto rest = super.serviceFacade.getRestaurantUserService().findRestaurentUser(restUserDto);
			
			RestaurantDto restDto = new RestaurantDto();
			restDto.setName(realName);//商家注册
			restDto.setMoilbe(userPhone);//电话
			restDto.setLinkman(userRelation);//联系人
			restDto.setUserId(rest.getId());//餐厅管理员Id
			
			super.serviceFacade.getRestaurantService().save(restDto);//保存餐厅
			List restList = super.serviceFacade.getRestaurantService().findByRestaurant(rest.getId());
			
			if(restList.size() > 0){
				restDto = (RestaurantDto) restList.get(0);
				restUserDto.setRestaurantId(restDto.getId());
				super.serviceFacade.getRestaurantUserService().update(restUserDto);
			}
			
			println("0");
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		} 
		return null;
	}
	
	/**
	 * 用户名验证是否存在
	 */
	public String userNameValidate() {
		boolean userNameBool = true;;
		try {
			userNameBool = super.serviceFacade.getUserService().validateUserName(userName);
			if (!userNameBool) { 
				println("-1"); 
			} else {
				println("0");
			}
		} catch (ServiceException e) {
			 logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		
		return null;
	}
	/**
	 * 商家注册验证
	 * @return
	 * @throws IOException 
	 */
	public String userValidate() throws IOException {
		/*String validateCodes = getRequest().getParameter("validateCode");
		String code = (String) getSession().getAttribute("validateCode");
		if(code.equals(validateCodes)){
			println(1);
			//return "user-validate";
		}else{
			println(-1);
		}*/
		return "user-validate";
	}
	
	/**
	 * 注册成功
	 */
	public String registerSuccess() {
		
		return "register-success";
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public int getUserSex() {
		return userSex;
	}

	public void setUserSex(int userSex) {
		this.userSex = userSex;
	}

	public String getUserIdNo() {
		return userIdNo;
	}

	public void setUserIdNo(String userIdNo) {
		this.userIdNo = userIdNo;
	}

	public String getUserMobile() {
		return userMobile;
	}

	public void setUserMobile(String userMobile) {
		this.userMobile = userMobile;
	}

	public String getUserPhone() {
		return userPhone;
	}

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

	public String getUserAddress() {
		return userAddress;
	}

	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}

	public String getUserZipCode() {
		return userZipCode;
	}

	public void setUserZipCode(String userZipCode) {
		this.userZipCode = userZipCode;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public Integer getUserType() {
		return userType;
	}

	public void setUserType(Integer userType) {
		this.userType = userType;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getUserRelation() {
		return userRelation;
	}

	public void setUserRelation(String userRelation) {
		this.userRelation = userRelation;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}
}
